package com.utf999.qiangmanger.dao.impl;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.utf999.qiangmanger.dao.AbstractRelationalDBDAO;
import com.utf999.qiangmanger.dao.iface.IStoreUserDAO;
import com.utf999.qiangmanger.entity.orm.StoreUserEntity;
import com.utf999.qiangmanger.exception.BizException;

/**
 * @author yinwenjie
 *
 */
@Component("StoreUserDAOImpl")
public class StoreUserDAOImpl extends AbstractRelationalDBDAO<StoreUserEntity> implements IStoreUserDAO {

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.AbstractRelationalDBDAO#getEntityClass()
	 */
	@Override
	protected Class<StoreUserEntity> getEntityClass() {
		return StoreUserEntity.class;
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.IStoreUserDAO#queryStoreuserByStoreid(java.lang.String)
	 */
	//TODO 未进行SQL优化，索引未建立
	@Override
	public List<StoreUserEntity> queryStoreuserByStoreid(String storeid , String workNumber , String userName , String realName) throws BizException {
		HashMap<String, Object> condition = new HashMap<String, Object>();
		condition.put("storeid", storeid);
		if(!StringUtils.isEmpty(workNumber)) {
			condition.put("workNumber", workNumber);
		}
		if(!StringUtils.isEmpty(userName)) {
			condition.put("userName", userName);
		}
		if(!StringUtils.isEmpty(realName)) {
			condition.put("realName", realName);
		}
		List<StoreUserEntity> users = this.queryByHqlFile("IStoreUserDAO.queryStoreuserByStoreid", condition);
		
		return users;
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.IStoreUserDAO#queryStoreuserByUsername(java.lang.String)
	 */
	//TODO 未进行SQL优化，索引未建立
	@Override
	public StoreUserEntity queryStoreuserByUsername(String username) throws BizException {
		HashMap<String, Object> condition = new HashMap<String, Object>();
		condition.put("userName", username);
		List<StoreUserEntity> users = this.queryByHqlFile("IStoreUserDAO.queryStoreuserByUsername", condition);
		if(users == null || users.size() == 0) {
			return null;
		}
		
		return users.get(0);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.IStoreUserDAO#queryStoreuserByWorknumber(java.lang.String, java.lang.String)
	 */
	@Override
	public StoreUserEntity queryStoreuserByWorknumber(String storeid, String workNumber) throws BizException {
		HashMap<String, Object> condition = new HashMap<String, Object>();
		condition.put("storeid", storeid);
		condition.put("workNumber", workNumber);
		List<StoreUserEntity> users = this.queryByHqlFile("IStoreUserDAO.queryStoreuserByWorknumber", condition);
		if(users == null || users.size() == 0) {
			return null;
		}
		
		return users.get(0);
	}	
}
